package com.viettel.core.common.utils;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

/**
 * The Class MapEditor.
 * @author LuanDV
 */
public class MapEditor {

    /** The Constant PERSONAL_LOCATION_TYPE_ID. */
    public static final int PERSONAL_LOCATION_TYPE_ID = 21;

    /**
     * Gets the point from coordinate.
     * 
     * @param lat
     *            the lat
     * @param lng
     *            the lng
     * @return the point from coordinate
     */
    public static Point getPointFromCoordinate(double lat, double lng) {

        if (lat < ListConst.MIN_LAT) {
            return null;
        }
        String coordinate = "POINT(" + lng + " " + lat + ")";
        WKTReader fromText = new WKTReader();
        Geometry geom = null;
        try {
            geom = fromText.read(coordinate);
        } catch (ParseException e) {
            throw new RuntimeException("Not a WKT string:" + coordinate);
        }

        geom.setSRID(ListConst.GEOM_SRID_DEFAULT);
        return (Point) geom;
    }
    
    /**
     * @author LuanDV
     * @since 02/12/2010 - Created.
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     * @throws Exception
     */
	public static double calculateDistance(double lat1, double lng1,
			double lat2, double lng2) throws Exception {
		int RADIO = 6371;
		double dLat = Math.toRadians(lat2-lat1);
		double dLng = Math.toRadians(lng1-lng2);
		
		double temp = Math.sin(dLat/2) * Math.sin(dLat/2) +
		        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
		        Math.sin(dLng/2) * Math.sin(dLng/2); 
		double temp2 = RADIO * 2 * Math.atan2(Math.sqrt(temp), Math.sqrt(1-temp)) * 1000; 
		return Math.round(temp2); //metre
	}
	
	/**
     * @author LuanDV
     * @since 02/12/2010 - Created.
	 * @param p1
	 * @param p2
	 * @return
	 * @throws Exception
	 */
	public static double calculateDistance(Point p1, Point p2) throws Exception {
		int RADIO = 6371;
		double lat1 = p1.getY();
		double lng1 = p1.getX();
		double lat2 = p2.getY();
		double lng2 = p2.getX();
		double dLat = Math.toRadians(lat2-lat1);
		double dLng = Math.toRadians(lng1-lng2);
		
		double temp = Math.sin(dLat/2) * Math.sin(dLat/2) +
		Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
		Math.sin(dLng/2) * Math.sin(dLng/2); 
		double temp2 = RADIO * 2 * Math.atan2(Math.sqrt(temp), Math.sqrt(1-temp)) * 1000; 
		return Math.round(temp2); //metre
	}
	
	
	public static void main(String[] args) throws Exception {
//		double dis = getPointFromCoordinate(10.78326914948947,106.69089317321777).distance(getPointFromCoordinate(10.781372054122457,106.6930603981018));
		//double dis = calculateDistance(getPointFromCoordinate(10.78326914948947,106.69089317321777),getPointFromCoordinate(10.781372054122457,106.6930603981018));
		//System.out.println(dis);
		//System.out.println(calculateDistance(10.78326914948947,106.69089317321777,10.781372054122457,106.6930603981018));
		System.out.println(Math.round(1500.516223212522));
	}
}
